
package sheep.controller;

/////////////////////////////////////////////////////////////////////////////

import java.awt.Color;
import java.awt.geom.RoundRectangle2D;
import java.util.Calendar;
import java.util.LinkedList;
import org.jdom.Element;
import sheep.model.Calendrier;
import sheep.model.Note;
import sheep.model.Project;
import sheep.model.Task;
import sheep.model.WorkGroup;
import sheep.model.Worker;
import sheep.utils.fileio.WorkspaceLoader;
import sheep.utils.fileio.WorkspaceSaver;
import sheep.view.Component.CalendarCellRenderer.Adapter;



/**
 *
 * @author geek
 */


public class Workspace {

  private LinkedList<Worker> workers = new LinkedList<Worker>();
  private LinkedList<WorkGroup> workGroups = new LinkedList<WorkGroup>();
  private String savePath;
  private String saveFile;
  
   Color tabColor[]= {new Color(0,0, 255),new Color(40,40, 255),new Color(40,40, 255),
                      new Color(80,80, 255),new Color(120,120, 255),new Color(160,160, 255),
                      new Color(190,190, 255)};
  
  
  public Object[][] getWorkersTab() {
    Object[][] result = new Object [getWorkersList().size()][Worker.workerTabLength];
    
    int i=0;
    for(Worker w : getWorkersList()) {
      result[i] = w.getWorkerTab();
      i++;
    }
    
    return result;
  }
  
  public Worker getWorker(Object id) {
    Worker result = null;
    
    for(Worker w : workers) {
      if(w.getId() == id )
        result = w;
    }
    
    return result;
  }
  
  public Worker getWorkerFromId(int id) {
    Worker result = null;
    
    for(Worker w : workers) {
      if(w.getId() == id)
        result = w;
    }
    
    return result;
  }
  
  public Calendrier getCalendrierFromId(int id) {
    Calendrier result = null;
    
    for(Calendrier c : this.calendarList) {
      if(c.getId() == id)
         result = c;
    }
    
    return result;
  }
  
  public Note getNoteFromId(int id) {
    Note result = null;
    
    for(Note n : this.noteList) {
      if(n.getId() == id)
        result = n;
    }
    
    return result;
  }
  
  public Worker getWorker(String firstName, String lastName) {
    Worker result = null;
    
    for(Worker w : workers) {
      if(w.getFirstName().equals(firstName) && w.getLastName().equals(lastName))
        result = w;
    }
    
    return result;
  }
  
  public WorkGroup getWorkGroup(Object id) {
    WorkGroup result = null;
    
    for(WorkGroup w : workGroups) {
      if(w.getId() == id )
        result = w;
    }
    
    return result;
  }
  
  public Object[][] getWorkGroupsTab() {
    Object[][] result = new Object[getWorkGroupsList().size()][WorkGroup.workGroupTabLength];
    
    int i =0;
    for(WorkGroup w : getWorkGroupsList()) {
      result[i] = w.getWorkGroupTab();
      i++;
    }
    
    return result;
  }
  
  public //Note testNote = new Note();
  LinkedList<Note> getNoteList() {
    return noteList;
  }

  public void setNoteList(LinkedList<Note> noteList) {
    this.noteList = noteList;
  }
  
  public //Note testNote = new Note();
  LinkedList<Note> getProjectNoteList() {
    return projectNoteList;
  }

  public void setProjectNoteList(LinkedList<Note> noteList) {
    this.projectNoteList = noteList;
  }
  
  public void generateProjectList(){
    //projectNoteList = (LinkedList<Note>)noteList.clone();
    projectNoteList = new LinkedList<Note>();
    
    int i = 0;
    for (Task t : projectList){
      Color c1 = tabColor[(i) % tabColor.length];
      i++;
      for(Task t1 : t.getTaskList(true, true)){
        
        Note note3 = new Note();
        if (t1.isCritical())
          note3.setColor(new Color (255,100,100));
        else  
          note3.setColor(c1);
        note3.setTask(t1);
        Calendar temp = Calendar.getInstance();
        temp.setTime(t1.getEarliestBeginTime().getTime());

        Calendar temp2 = Calendar.getInstance();
        temp2.setTime(temp.getTime());
        temp2.add(Calendar.HOUR_OF_DAY,1);
  
        note3.setDate(temp);
        note3.setEndDate(temp2);
        
        note3.setTitle(t1.getName() +" : début");
        projectNoteList.add(note3);
        
        Note note4 = new Note();
        if (t1.isCritical())
          note4.setColor(new Color (255,100,100));
        else  
          note4.setColor(c1);
        
        note4.setTask(t1);
        Calendar temp3 = Calendar.getInstance();
        temp3.setTime(t1.getEarliestBeginTime().getTime());

        Calendar temp4 = Calendar.getInstance();
        temp4.setTime(temp3.getTime());
        temp4.add(Calendar.HOUR_OF_DAY,1);
  
        note4.setDate(temp3);
        note4.setEndDate(temp4);
        
        note4.setTitle(t1.getName() +" : fin");
        projectNoteList.add(note4);        

        
      }
        
    }
    
  //projectNoteList = (LinkedList<Note>)tempRet.clone();  
  }
  
    private Note SelectedNote;
   
    private Note projectSelectedNote;
    
        // Calendier predefinis
    public LinkedList<Calendrier> calendarList = new LinkedList<Calendrier>();

    
   private Calendrier currentCalendrier;
 /////////////////////////////////////////////////////////////////////////////

 
  private LinkedList<Note> noteList = new LinkedList<Note>();
  private LinkedList<Note> projectNoteList = new LinkedList<Note>();
  private Calendar currentDay;  
  
  private LinkedList<Task> projectList = new LinkedList<Task>();
  private Task currentProject;    
  
  private Task currentTask; 
  private Task selectedTask; 
  
  private Worker currentWorker;
  /////////////////////////////////////////////////////////////////////////////
  
  public Workspace(){
    currentDay = Calendar.getInstance();
    calendarList.add(new Calendrier("General", Adapter.Status.SELECTED, null, null, new Color (0,102,102)));
    currentCalendrier = calendarList.getLast();
    calendarList.getLast().setCurrent(true);
    /*calendarList.add(new Calendrier("Travail", Adapter.Status.SELECTED, null, null, new Color (204,0,51)));
    calendarList.add(new Calendrier("Loisirs", Adapter.Status.SELECTED, null, null, new Color (0,0,153)));
    calendarList.add(new Calendrier("Famille", Adapter.Status.SELECTED, null, null , new Color (53,93,53)));
    currentCalendrier = calendarList.getFirst();
    calendarList.getFirst().setCurrent(true);
    Note note = new Note();
    note.setCalendrier(currentCalendrier);
    //noteList.add(note);
    //currentCalendrier.addNote(note);
    
    Note note2 = new Note();
    note2.setDate(Calendar.getInstance());
    note2.getDate().add(Calendar.DAY_OF_YEAR, 1);
    note2.getDate().add(Calendar.HOUR_OF_DAY, -4); 
    note2.getEndDate().add(Calendar.DAY_OF_YEAR, 1);
    note2.getEndDate().add(Calendar.HOUR_OF_DAY, -4);    
    note2.setCalendrier(currentCalendrier);
    
    note2.setTitle("note 2");
    //noteList.add(note2);
    //currentCalendrier.addNote(note2);
    
    Note note3 = new Note();
    note3.setDate(Calendar.getInstance());
    note3.getDate().add(Calendar.DAY_OF_YEAR, 3);
    note3.getDate().add(Calendar.HOUR_OF_DAY, 2);
    note3.getEndDate().add(Calendar.DAY_OF_YEAR, 3);
    note3.getEndDate().add(Calendar.HOUR_OF_DAY, 2);
    note3.setCalendrier(currentCalendrier);
    note3.setTitle("note 3");
    //noteList.add(note3);
    //currentCalendrier.addNote(note3);*/
  }

  public Note getSelectedNote() {
    return SelectedNote;
  }

  public void setSelectedNote(Note SelectedNote) {
    this.SelectedNote = SelectedNote;
  }
  
  public Note getProjectSelectedNote() {
    return projectSelectedNote;
  }

  public void setProjectSelectedNote(Note SelectedNote) {
    this.projectSelectedNote = SelectedNote;
  }
  
  
  public LinkedList<Calendrier> getCalendarList() {
    return calendarList;
  }

  public Calendrier[] getCalendarTab(){
    Calendrier tab []= new Calendrier[calendarList.size()];
    int i = 0;
    for (Calendrier cal : calendarList){
      tab[i]= cal;
      i++; 
    }
    return tab;
  }
  public void setCalendarList(LinkedList<Calendrier> calendarList) {
    this.calendarList = new LinkedList<Calendrier>(calendarList);
  }

  public Calendrier getCurrentCalendrier() {
    return currentCalendrier;
  }

  public void setCurrentCalendrier(Calendrier currentCalendrier) {
    this.currentCalendrier = currentCalendrier;
  }

  public Calendar getCurrentDay() {
    return currentDay;
  }

  public void setCurrentDay(Calendar currentDay) {
    this.currentDay = currentDay;
  }

  public LinkedList<Worker> getWorkersList() {
    return workers;
  }
  
  public void setWorkersList(LinkedList<Worker> list) { this.workers = list; }

  public LinkedList<WorkGroup> getWorkGroupsList() {
    return workGroups;
  }
  
  public void setWorkGroupsList(LinkedList<WorkGroup> list) { this.workGroups = list; }

  
  
  public void setWorkers(LinkedList<Worker> workers) {
    this.workers = workers;
  }

  public LinkedList<Task> getProjectList() {
    return projectList;
  }

  public String[] getProjectTab() {
    String[] tab = new String[projectList.size()];
    int i = 0;
    for (Task t : projectList){
      tab[i]= t.getName();
      i++;
    }
    return tab;
  }  
  
  public void setProjectList(LinkedList<Task> projectList) {
    this.projectList = projectList;
  }

  public Task getCurrentProject() {
    return currentProject;
  }

  public void setCurrentProject(Task currentProject) {
    this.currentProject = currentProject;
  }

  public Task getCurrentTask() {
    return currentTask;
  }

  public void setCurrentTask(Task currentTask) {
    this.currentTask = currentTask;
  }

  public Task getSelectedTask() {
    return selectedTask;
  }

  public void setSelectedTask(Task selectedTask) {
    this.selectedTask = selectedTask;
  }
  
  public void saveWorkspace() {
    WorkspaceSaver ws = new WorkspaceSaver(this, this.getSavePath() + this.getSaveFile());
    Element e = ws.generateWorkspace();
    ws.save(e);
  }
  
  public void openWorkspace() {
    WorkspaceLoader wl = new WorkspaceLoader(this, this.getSavePath() + this.getSaveFile());
    wl.loadWorkspace();
  }

  public String getSavePath() {
    return savePath;
  }

  public void setSavePath(String savePath) {
    this.savePath = savePath;
  }

  public String getSaveFile() {
    return saveFile;
  }

  public void setSaveFile(String saveFile) {
    this.saveFile = saveFile;
  }

  public Worker getCurrentWorker() {
    return currentWorker;
  }

  public void setCurrentWorker(Worker currentWorker) {
    this.currentWorker = currentWorker;
  }
}
